package com.starview.star.operators.misc.runtime;

import com.starview.platform.data.EvaluationException;
import com.starview.platform.data.IFunction;
import com.starview.platform.data.IValue;
import com.starview.platform.data.type.IType;
import com.starview.platform.data.type.Refresher;
import com.starview.platform.data.type.StandardTypes;
import com.starview.platform.data.type.TypeUtils;
import com.starview.platform.data.type.TypeVar;
import com.starview.platform.data.value.BoolWrap;
import com.starview.platform.data.value.Factory;
import com.starview.star.operators.CafeEnter;

public class MiscOps
{

  public static class IsNull implements IFunction
  {

    @CafeEnter
    public static BoolWrap enter(IValue arg)
    {
      return Factory.newBool(arg == null);
    }

    public static final String name = "__isNull";

    @Override
    public IValue enter(IValue... args) throws EvaluationException
    {
      return enter(args[0]);
    }

    @Override
    public IType getType()
    {
      return type();
    }

    public static IType type()
    {
      return Refresher.generalize(TypeUtils.functionType(new TypeVar(), StandardTypes.booleanType));
    }
  }

  public static class Id implements IFunction
  {
    public static final String name = "__id";

    @CafeEnter
    public static IValue enter(IValue x)
    {
      return x;
    }

    @Override
    public IValue enter(IValue... args) throws EvaluationException
    {
      return enter(args[0]);
    }

    @Override
    public IType getType()
    {
      return funType();
    }

    public static IType funType()
    {
      TypeVar t = new TypeVar();
      return Refresher.generalize(TypeUtils.functionType(t, t));
    }
  }
}
